package service

import (
	"errors"
	"gorm.io/gorm"
	"mojieshi/define"
	"mojieshi/models"
	"sync"
	"time"
)

var m sync.RWMutex

func (l mallLogic) PayMiniCallBack(notification models.PayCallbackNotification) error {
	m.Lock()
	defer m.Unlock()
	if notification.IsOK() == false {
		return errors.New("未知错误")
	}
	var orderInfo models.MPayOrder
	err := models.DB.Model(&orderInfo).Where("Ordernum=?", notification.OutTradeNo).First(&orderInfo).Error
	if err != nil {
		return err
	}
	if orderInfo.Status > 0 {
		// 重复的通知 不用处理
		return nil
	}
	//c处理支付时间
	completeTime, _ := time.ParseInLocation("20060102150405", notification.TimeEnd, time.Local)
	//更新订单状态
	tx := models.DB.Begin()
	//更新支付表状态
	tx.Model(&orderInfo).Where("id = ?", orderInfo.Id).Updates(&models.MPayOrder{
		Status:  1,
		Paytime: int(completeTime.Unix()),
		TradeNo: notification.TransactionID,
	})
	if err != nil {
		tx.Rollback()
		return err
	}
	//判断更新支付类型订单状态
	if orderInfo.Type == 2 { //更新余额充值状态
		err = RechargeDetails(tx, orderInfo, notification.TransactionID, completeTime.Unix())
		if err != nil {
			tx.Rollback()
			return err
		}
	} else if orderInfo.Type == 1 { //更新服务下单状态
		err = FuWuDetails(tx, orderInfo, notification.TransactionID, completeTime.Unix())
		if err != nil {
			tx.Rollback()
			return err
		}
	}
	tx.Commit()
	return nil
}

// 服务下单支付成功后调用
func FuWuDetails(tx *gorm.DB, order models.MPayOrder, TransactionID string, completeTime int64) error {
	//更新服务总表状态
	var editData = map[string]interface{}{
		"status":  1,
		"paytime": int(completeTime),
		"paynum":  TransactionID,
	}
	err := tx.Model(new(models.MFuwuOrder)).Where("id=?", order.Orderid).Updates(&editData).Error
	if err != nil {
		return err
	}
	//查询服务订单详情
	var orderData models.MFuwuOrder
	err = tx.Model(new(models.MFuwuOrder)).Where("id=?", order.Orderid).First(&orderData).Error
	if err != nil {
		return err
	}
	//判断是否给用户增加积分
	if orderData.Givescore > 0 {
		cur := models.ScoreAdd(tx, orderData.Mid, orderData.Givescore, orderData.Aid, "购物赠送积分，订单号:"+orderData.Ordernum)
		if cur == 2 {
			return errors.New("积分增加错误！")
		}
	}
	//更新服务套餐记录表
	var ediFuData = map[string]interface{}{
		"status": 1,
	}
	err = tx.Model(new(models.MFuwuOrderLog)).Where("orderid=?", order.Orderid).Updates(&ediFuData).Error
	if err != nil {
		return err
	}
	//写入捐款
	if define.FloatDecimal(orderData.JuanPrice) > 0 {
		err = models.JuanAdd(tx, orderData.JuanPrice, orderData.Totalprice, orderData.Mid, orderData.Ordernum, orderData.ID, orderData.Aid)
		if err != nil {
			return err
		}
	}
	return nil
}

// 余额充值支付成功后调用
func RechargeDetails(tx *gorm.DB, order models.MPayOrder, TransactionID string, completeTime int64) error {
	//更新充值订单状态
	var editData = map[string]interface{}{
		"status":  1,
		"paytime": int(completeTime),
		"paynum":  TransactionID,
	}
	err := tx.Model(new(models.MRechargeOrder)).Where("id=?", order.Orderid).Updates(&editData).Error
	if err != nil {
		return err
	}
	var rechargeData models.MRechargeOrder
	err = tx.Model(new(models.MRechargeOrder)).Where("id=?", order.Orderid).First(&rechargeData).Error
	if err != nil {
		return err
	}
	//查询用户信息
	var MemData models.MMember
	err = tx.Model(new(models.MMember)).Where("id=?", order.Mid).First(&MemData).Error
	if err != nil {
		return err
	}
	//写入用户表models.MMember
	Newmoney := define.AddDecimal(rechargeData.Money, rechargeData.Give)
	if rechargeData.GiveScore > 0 {
		cur := models.ScoreAdd(tx, rechargeData.Mid, rechargeData.GiveScore, rechargeData.Aid, "余额充值赠送积分，订单号:"+rechargeData.Ordernum)
		if cur == 2 {
			return errors.New("积分增加错误！")
		}
	}
	if err != nil {
		return err
	}
	//写入余额记录
	cur := models.MoneyAdd(tx, rechargeData.Mid, Newmoney, rechargeData.Aid, "余额充值，订单号:"+rechargeData.Ordernum)
	if cur == 2 {
		return errors.New("余额充值错误！")
	}
	return nil
}
